package com.art.sunflower.controller;

import com.art.sunflower.exception.NullCourseException;
import com.art.sunflower.mapper.CourseMapper;
import com.art.sunflower.pojo.Course;
import com.art.sunflower.pojo.User;
import com.art.sunflower.service.CourseService;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping(value = "/course")
@Api(value = "课程控制器")
public class CourseController {
    @Resource
    CourseService courseService;
    @Resource
    CourseMapper courseMapper;


    @ResponseBody
    @RequiresRoles(value = {"master"})
    @GetMapping()
    @ApiOperation("分页获取课程信息列表")
    public Message findAllCourse(@RequestParam Integer pagenum, @RequestParam Integer pagesize) {

        return Message.createSuc(courseService.findAllCourse(pagenum, pagesize));

    }

    @ResponseBody
    @RequiresRoles(value = {"master"})
    @GetMapping("/{id}")
    @ApiOperation("通过id获取课程信息")
    public Message getOneCourse(@PathVariable int id) {
        if (courseService.selectOneCourse(id) == null) {
            return Message.createErr("课程不存在，查找失败");
        } else {
            return Message.createSuc(courseService.selectOneCourse(id));
        }
    }

    @ResponseBody
    @RequiresRoles(value = {"master"})
    @DeleteMapping("/{id}")
    @ApiOperation("删除一条课程")
    public Message deleteCourse(@PathVariable int id) {

        if (courseService.deleteById(id) == 1) {
            return Message.createSuc("删除成功");//删除成功
        }
        return Message.createErr("删除失败");//删除失败
    }

    @ResponseBody
    @RequiresRoles(value = {"master"})
    @PostMapping()
    @ApiOperation("添加课程")
    public Message addCourse(@RequestParam("course_name") String courseName,
                             @RequestParam("introduce") String introduce, @RequestParam("requirement") String requirement, @RequestParam("teach_fee") int teachFee,
                             @RequestParam("material_fee") int materialFee, @RequestParam("cooperation_id") int cooperationId) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();

        Course course = new Course();
        course.setCourseName(courseName);
        course.setIntroduce(introduce);
        course.setRequirement(requirement);
        course.setMaterialFee(materialFee);
        course.setTeachFee(teachFee);
        course.setCooperationId(cooperationId);
        course.setMasterId(user.getId());
        System.out.println(course.toString());
        if (courseService.addCourse(course) == 1) {
            return Message.createSuc("添加成功");//添加成功
        }
        return Message.createErr("添加失败");//添加失败
    }

    @ResponseBody
    @RequiresRoles(value = {"master"})
    @PutMapping()
    @ApiOperation("修改课程信息")
    public Message updateCourse(@RequestParam("id") int id, @RequestParam("course_name") String courseName,
                                @RequestParam("introduce") String introduce, @RequestParam("requirement") String requirement, @RequestParam("teach_fee") int teachFee,
                                @RequestParam("material_fee") int materialFee, @RequestParam("cooperation_id") int cooperationId) {
        if (courseService.updateCourseById(id, courseName, introduce, requirement, teachFee, materialFee, cooperationId) == 1) {
            return Message.createSuc("修改成功");//修改成功
        }
        return Message.createErr("修改失败");//添加失败
    }

    @RequiresRoles(value = {"master"})
    @ApiOperation(value = "获取课程名字列表")
    @GetMapping(value = "/name")
    public Message getNameList() {
        try {
            return Message.createSuc(courseService.getNameList());
        } catch (NullCourseException e) {
            e.printStackTrace();
            return Message.createErr("课程不存在");
        }
    }



   /* @ResponseBody
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("courseByAdmin")
    @ApiOperation("删除一条课程")
    public Message deleteCourseByAdmin(@RequestParam("id") int id) {

        if (courseService.deleteCourseByAdmin(id) == 1) {
            return Message.createSuc("删除成功");//删除成功
        }
        return Message.createErr("删除失败");//删除失败
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @PostMapping("courseByAdmin")
    @ApiOperation("添加课程")
    public Message addCourseByAdmin(@RequestParam("master_id") int masterId, @RequestParam("course_name") String courseName,
                                    @RequestParam("introduce") String introduce, @RequestParam("requirement") String requirement, @RequestParam("teach_fee") int teachFee,
                                    @RequestParam("material_fee") int materialFee, @RequestParam("cooperation_id") int cooperationId) {
        if (courseService.addCourseByAdmin(masterId, courseName, introduce, requirement, teachFee, materialFee, cooperationId) == 1) {
            return Message.createSuc("添加成功");//添加成功
        }
        return Message.createErr("添加失败");//添加失败
    }*/

}
